package com.partners.controller.Page;

import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.PermissionEnum;
import com.partners.dao.bo.AddressDO;
import com.partners.service.AddressService;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 积分商城页面展示
 * @author liangyongle
 * @date 2019-01-07
 */
@Controller
@RequestMapping("productPage")
@Slf4j
public class ProductPageController {

    @Resource
    private UserService userService;

    @Resource
    private SessionManager sessionManager;


    /**
     * 产品列表
     * @param session HttpSession
     * return
     * */
    @GetMapping("productList")
    @Auths
    public ModelAndView productList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        Integer code = PermissionEnum.PRODUCT_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/product_list";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     *获取兑换管理列表
     *
    * */
    @GetMapping("exchangeList")
    @Auths
    public ModelAndView exchangeList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        Integer code = PermissionEnum.EXCHANGE_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/exchange_list";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;

    }

    /**
     *编辑产品
     *
     * */
    @GetMapping("productEdit")
    @Auths
    public  ModelAndView productEdit(@RequestParam(value="id", required=false) Integer id,HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        id = id != null?id:0;
        Integer code = PermissionEnum.PRODUCT_EDIT.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/product_edit";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("id",id);
        return mav;
    }

    /**
    * 兑换产品页面
    * */
    @GetMapping("exchange/{id}")
    @Auths
    public ModelAndView exchangeProduct(HttpSession session,@PathVariable("id") Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        id = id != null?id:0;
        Integer code = PermissionEnum.PRODUCT_EXCHANGE.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/exchange_product";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("id",id);
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     * 产品详情
     * */
    @GetMapping("product_detail")
    public ModelAndView productDetail(@RequestParam("id") Integer id){
        ModelAndView mav =new ModelAndView("show/product_detail");
        mav.addObject("id",id);
        return mav;
    }


}

